import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';


@Injectable({
  providedIn: 'root'
})
export class ProcessService {

  constructor(private http:HttpClient) { }
  private Process = '/Process';
  private createPaginationParams(currentPage: number, itemsPerPage: number): HttpParams {
    return new HttpParams()
      .set('page', currentPage.toString())
      .set('pageSize', itemsPerPage.toString());
  }// 创建分页参数
  GetPagedProcesss(SearchTerm?:any,page?: number, pageSize?: number){
    let params = new HttpParams();
    if (SearchTerm ) {
      params = params.set('SearchTerm', SearchTerm);
    }
    if (page !== undefined && pageSize !== undefined) {
      params = params.set('PageNumber', page.toString());
      params = params.set('PageSize', pageSize.toString());
    }
    return this.http.get(environment.apiUrl+ this.Process+'/GetProcesss',{
      params: params
    })
  }// 获取所有工序
  CreateProcess(data:any){
    return this.http.post(environment.apiUrl+ this.Process+'/CreateProcess',data)
  }// 创建工序
  GetProcessById(id:any){
    return this.http.get(environment.apiUrl+ this.Process+'/GetProcessById?id='+id)
  }// 获取工序
  DeleteProcesss(ids:any){
    const url = this.Process+'/DeleteProcesss';
    const options = {
      body: ids
    };
    return this.http.delete(environment.apiUrl+ url,options);
  }// 批量删除工序

  UpdateProcess(id:any,data:any){
    return this.http.put(environment.apiUrl+ this.Process+'/UpdateProcess',data,{params:{id:id}})
  }// 更新工序

  GetTechnologyRoutes(SearchTerm?:any,page?: number, pageSize?: number){
    let params = new HttpParams();
    if (SearchTerm ) {
      params = params.set('SearchTerm', SearchTerm);
    }
    if (page !== undefined && pageSize !== undefined) {
      params = params.set('PageNumber', page.toString());
      params = params.set('PageSize', pageSize.toString());
    }
    return this.http.get(environment.apiUrl+ this.Process+'/GetTechnologyRoutes',{
      params: params
    })
  }// 获取所有工艺路线
  DeleteTechnologyRoutes(ids:any){
    const url = this.Process+'/DeleteTechnologyRoutes';
    const options = {
      body: ids
    };
    return this.http.delete(environment.apiUrl+ url,options);
  }// 批量删除工艺路线
  CreateTechnologyRoute(data:any){
    return this.http.post(environment.apiUrl+ this.Process+'/CreateTechnologyRoute',data)
  }// 创建工艺路线
  GetTechnologyRouteById(id:any){
    return this.http.get(environment.apiUrl+ this.Process+'/GetTechnologyRouteById',{params:{id:id}})
  }// 获取工艺路线
  UpdateTechnologyRoute(id:any, data:any){
    return this.http.put(environment.apiUrl+ this.Process+'/UpdateTechnologyRoute',data ,{params:{id:id}})
  }// 更新工艺路线
  UploadAttachment(file:File){
    const url = this.Process+'/UploadAttachment';
    const formData = new FormData();
    formData.append('file', file);
    return this.http.post(environment.apiUrl+ url,formData);
  }// 上传附件
  DownloadProcessFileModel(){
    return this.http.get(environment.apiUrl+'/Excel'+'/DownloadProcessFileModel',{responseType: 'blob'})
  }// 下载工序模板
  DownloadProcessFile(){
    return this.http.get(environment.apiUrl+'/Excel'+'/DownloadProcessFile',{responseType: 'blob'})
  }// 下载工序文件
  ImportProcessesFromExcel(file:File){
    const url = '/Excel'+'/ImportProcessesFromExcel';
    const formData = new FormData();
    formData.append('file', file);
    return this.http.post(environment.apiUrl+ url,formData);
  }
}
